﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Cors;
using Brc_PaaS.Api.Common;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.Redis;
using Brc_PaaS.Core.Redis;
using System.Collections.Generic;
using Brc_PaaS.Bll.System;
using Brc_PaaS.Api.Filters;
using System.ComponentModel.DataAnnotations;

namespace Brc_PaaS.Api.Controllers.Redis
{
    /// <summary>
    /// Redis操作Hash控制器
    /// </summary>
    [Route("api/redis/[controller]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "Redis")]
    public class RedisHashController : RedisBaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="redis"></param>
        /// <param name="system"></param>
        public RedisHashController(IConfiguration configuration, Brc_PaaS.Bll.Redis.IRedis redis, ISystem system) : base(configuration, redis, system)
        {

        }
        /// <summary>
        /// 插入哈希对象
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<BaseResponseModel> Post(RedisHashModel model)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && model != null)
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    redisManager.HashMultiSet(model.Key, model.DicValue, model.TimeSpan);
                }
            }
            result.IsSuccess = true;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }

        /// <summary>
        /// 获取整个哈希对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ResponseModel<Dictionary<string, string>>> Get([Required(ErrorMessage = "键不能为空")]string key)
        {
            ResponseModel<Dictionary<string, string>> result = new ResponseModel<Dictionary<string, string>>() { IsSuccess = false, Msg = string.Empty, Data = new Dictionary<string, string>() };
            if (SystemInfo != null && BusinessRedisServerEntity != null && !string.IsNullOrEmpty(key))
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.Data = redisManager.HashGetAll(key);
                }
            }
            result.IsSuccess = true;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }      
    }
}